package com.nexgo.payment.pack;

import com.nexgo.common.ByteUtils;
import com.nexgo.data.entity.SendData;
import com.nexgo.data.entity.SendDataBuilder;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.constrant.TransType;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.databases.TransRecord;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.util.MessageHelper;
import com.nexgo.payment.util.StringUtils;

/**
 * 脚本结果通知
 *
 * Created by xiaox on 2017/8/16.
 */
class ScriptNotifyBuilder extends DataBuilder {
    private TransRecord mTransRecord;
    private TransType mOrgTransType;

    ScriptNotifyBuilder(TransData transData, DataSource repository) {
        super(transData, repository);
        mTransRecord = mRepository.queryTransRecord(mTransData.getOrgTraceNum());
        mOrgTransType = TransType.values()[mTransRecord.getTransType()];
    }

    @Override
    SendData form() {
        SendData sendData = new SendDataBuilder().setListener(mListener).setSignListener(mSignListener).createSendData();
        sendData.setHeader(MessageHelper.formMessageHeader(mRepository.getParamValue(PreferencesConstants.TPDU)));
        sendData.setMsgType(mTransData.getTransType().getMsgType());
//        wzp add
        sendData.setPan(mTransRecord.getPan());
        sendData.setProcCode(mTransData.getTransType().getProcCode());//lr 交行3域送100000
//        sendData.setAmount(mTransRecord.getAmount());
        sendData.setTraceNum(mRepository.getParamValue(PreferencesConstants.TRACE_NUMBER));
        mRepository.increaseTraceNumber();
//        添加12 13
        sendData.setTime(mTransRecord.getTime());
        sendData.setDate(mTransRecord.getDate());

//        sendData.setEntryMode(mTransRecord.getEntryMode());
//        sendData.setCsn(mTransRecord.getCsn());
//        sendData.setAcceptCode(mTransRecord.getAcceptCode());
        sendData.setRefNum(mTransRecord.getRefNum());
//        sendData.setAuthCode(mTransRecord.getAuthCode());
        sendData.setTerminalId(mTransRecord.getTerminalId());
        sendData.setMerchantId(mTransRecord.getMerchantId());
//        sendData.setFundType(mTransRecord.getFundType());
        sendData.setEmvTlv(mTransRecord.getScriptTlv());
        sendData.setField60(mTransRecord.getField60());
//        sendData.setField61(formField61());
        sendData.setMac("0000000000000000");
        return sendData;
    }



    private String formField60() {
        StringBuilder sb = new StringBuilder();
        sb.append(mTransData.getTransType().getFuncCode());
        sb.append(mRepository.getParamValue(PreferencesConstants.BATCH_NUMBER));
        sb.append("951");
        return ByteUtils.byteArray2HexString(sb.toString().getBytes());
    }

    private String formField61() {
        StringBuilder sb = new StringBuilder();
        sb.append(StringUtils.firstNonEmpty(mTransRecord.getTraceNum(), "000000"));
        sb.append(StringUtils.firstNonEmpty(mTransRecord.getBatchNum(), "000000"));
        sb.append(StringUtils.firstNonEmpty(mTransRecord.getDate(), "0000"));
        return sb.toString();
    }
}
